from flask import Flask, request, jsonify
import sever  # 确保你有这个模块，否则你需要安装或移除这个import
import re
import json
import sever_wang
app = Flask(__name__)
@app.route('/', methods=['POST'])  # 只处理POST请求
def home():
    if request.method == 'POST':
        # 假设客户端发送的是JSON格式的请求体
        if request.is_json:
            data = request.json  # 获取JSON数据
            category = data.get('category')
            print(category)
            if(category=="1"):
                print("进入试卷批改")
                imgUrl = data.get('imgUrl')
                subjectName = data.get('subjectName')#学科名字
                print(subjectName)
                gradingStandard = data.get('gradingStandard')#改卷标准
                standardAnswer = data.get('standardAnswer')#标准答案
                score = data.get('score')#本题总分

                answerText = data.get('answerText')#手写
                # 在这里使用获取到的数据执行你的逻辑
                # 例如，你可以调用sever模块中的函数
                # 返回响应
                print(standardAnswer)
                s = sever_wang.se_read(subjectName,gradingStandard,standardAnswer,imgUrl,answerText,score)
                print(s)
                match = re.search(r'\[(.*?)\]', s, re.DOTALL)
                if match:
                    content_inside_brackets = match.group(0)
                    return content_inside_brackets
                else:
                    return {}
            if(category=="2"):
                print("开始进行学情分析")
                data_my  = data.get('data')
                print(data_my)
                data_my = json.loads(data_my)
                uid  = data.get('userId')
                print(data_my)
                print(uid)
                return sever_wang.xueqing_1(uid,data_my)
            if(category=="3"):
                imgUrl = data.get('imgUrl')
                uid = data.get('paperId')

                return sever_wang.shi_juan(uid,imgUrl)
        else:
            return 'Error: Invalid request format. Please send JSON data.', 400
    else:
        return 'This route only supports POST requests.', 405  # 如果不是POST请求，返回405 Method Not Allowed
if __name__ == '__main__':
    app.run(host="0.0.0.0", port=8080)